using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace ICP_MS.Plugin.Experiment;

public class FormSeqExperiment : Form, IExperiment
{
	private List<ExperimentDatasSeq> DatasSeqInForm;

	public SeqExperiment seqExperiment = new SeqExperiment();

	private bool isSaved = false;

	public static bool IsRun;

	private FormSeqConfig formSeqConfig = new FormSeqConfig();

	private FormSeqSamples formSeqSamples = new FormSeqSamples();

	private IContainer components = null;

	private ToolStrip toolStrip1;

	private ToolStripButton toolStripButton1;

	private ToolStripButton toolStripButton2;

	private Panel panel1;

	private ToolStripSeparator toolStripSeparator1;

	private ToolStripButton toolStripButton3;

	public int CurrentStatus => seqExperiment.currentStatus;

	public event DelegateExpStarted ExpStarted = null;

	public event DelegateDataChanged RefreshResultEvent = null;

	public event DelegateExpProgress ExpEvent = null;

	public event EventHandler<ExpNameArgs> updateToolstrip = null;

	public static event EventHandler analyseFinish;

	public bool PreRun()
	{
		return seqExperiment.PreRun();
	}

	public int StartAnalysis()
	{
		if (Detector.Instance.ExperimentState != -1)
		{
			if (Detector.Instance.ExperimentState == 5)
			{
				MessageBox.Show(Lang.Txt("AddFormExperiment.WaitTest"), Lang.Txt("AddFormExperiment.Warn"), MessageBoxButtons.OK);
				return 0;
			}
			if (MessageBox.Show(Lang.Txt("AddFormExperiment.StopOtherTest"), Lang.Txt("AddFormExperiment.Warn"), MessageBoxButtons.YesNo) != DialogResult.Yes)
			{
				return 0;
			}
			Detector.Instance.ExperimentStateChange(Detector.Instance.ExperimentState);
		}
		IsRun = true;
		seqExperiment.StartRun();
		return 0;
	}

	public static void OnAnalysisFinish()
	{
		IsRun = false;
		if (FormSeqExperiment.analyseFinish != null)
		{
			FormSeqExperiment.analyseFinish(null, null);
		}
	}

	private void Experiment_EventExperiment(object sender, ExperimentEventArgs e)
	{
		ExperimentDatas experiment = seqExperiment.DatasSeqs[seqExperiment.experimentIndex].experiment;
		SamplesInfos sample = e.Sample;
		int stepIndex = e.StepIndex;
		int num = stepIndex;
		if (num == 6 && this.RefreshResultEvent != null)
		{
			this.RefreshResultEvent(null, new ExpDataArgs(seqExperiment.DatasSeqs[seqExperiment.experimentIndex]));
		}
	}

	public void OnRefreshResult()
	{
		if (this.RefreshResultEvent != null)
		{
			this.RefreshResultEvent(null, new ExpDataArgs(seqExperiment.DatasSeqs[seqExperiment.experimentIndex]));
		}
	}

	private void OnAnalyzeFinished()
	{
		if (FormSeqExperiment.analyseFinish != null)
		{
			FormSeqExperiment.analyseFinish(null, null);
		}
	}

	private void ShowProgressStatus(string msg, int percent)
	{
		if (this.ExpEvent != null)
		{
			this.ExpEvent(this, new ExpProgressArgs(msg, percent));
		}
	}

	private void PerformProgressStep(ProgressBar bar)
	{
		Invoke((MethodInvoker)delegate
		{
			bar.PerformStep();
		});
	}

	public void StopAnalysis()
	{
		IsRun = false;
		if (seqExperiment.IsRun)
		{
			seqExperiment.IsRun = false;
			if (seqExperiment.DatasSeqs[seqExperiment.experimentIndex].experiment.IsRun)
			{
				seqExperiment.DatasSeqs[seqExperiment.experimentIndex].experiment.StopRun();
				ShowProgressStatus(Lang.Txt("FormExperiment.Status.EndAnalysis.Text"), 0);
			}
		}
	}

	public FormSeqExperiment(List<ExperimentDatasSeq> datasSeqInForm)
	{
		DatasSeqInForm = datasSeqInForm;
		InitializeComponent();
	}

	private void FormSeqExperiment_Load(object sender, EventArgs e)
	{
		base.WindowState = FormWindowState.Maximized;
		toolStripButton1.PerformClick();
		seqExperiment.analyseFinish += SeqExperiment_analyseFinish;
		ApplyResources();
	}

	private void ApplyResources()
	{
		toolStripButton1.Text = Lang.Txt("SeaExperiment.MethodList.Text");
		toolStripButton2.Text = Lang.Txt("SeaExperiment.SampleList.Text");
		toolStripButton3.Text = Lang.Txt("SeaExperiment.SampleResults.Text");
		Text = Lang.Txt("iMass.Experiment.SeqExperiment.Text");
	}

	private void SeqExperiment_analyseFinish(object sender, EventArgs e)
	{
		if (FormSeqExperiment.analyseFinish != null)
		{
			FormSeqExperiment.analyseFinish(null, null);
		}
		Invoke((MethodInvoker)delegate
		{
			MessageBox.Show(Lang.Txt("FormExperiment.Status.EndAnalysis.Text"), "提示", MessageBoxButtons.OK);
		});
	}

	private void toolStripButton1_Click(object sender, EventArgs e)
	{
		formSeqConfig.SetParam(DatasSeqInForm, seqExperiment);
		formSeqConfig.TopLevel = false;
		formSeqConfig.Parent = panel1;
		formSeqConfig.Dock = DockStyle.Fill;
		formSeqConfig.FormBorderStyle = FormBorderStyle.None;
		formSeqConfig.Show();
		formSeqConfig.Activate();
		formSeqConfig.BringToFront();
		formSeqConfig.Parent = panel1;
		Color color = Color.Black;
		Font font = new Font("Microsoft YaHei", 9f, FontStyle.Regular);
		toolStripButton2.ForeColor = color;
		toolStripButton2.Font = font;
		toolStripButton2.Checked = false;
		(sender as ToolStripButton).ForeColor = Color.Red;
		(sender as ToolStripButton).Font = new Font("Microsoft YaHei", 9f, FontStyle.Bold);
		(sender as ToolStripButton).Checked = true;
	}

	private void toolStripButton2_Click(object sender, EventArgs e)
	{
		formSeqSamples.SetParam(seqExperiment);
		formSeqSamples.TopLevel = false;
		formSeqSamples.Parent = panel1;
		formSeqSamples.Dock = DockStyle.Fill;
		formSeqSamples.FormBorderStyle = FormBorderStyle.None;
		formSeqSamples.Show();
		formSeqSamples.Activate();
		formSeqSamples.BringToFront();
		formSeqSamples.Parent = panel1;
		Color color = Color.Black;
		Font font = new Font("Microsoft YaHei", 9f, FontStyle.Regular);
		toolStripButton1.ForeColor = color;
		toolStripButton1.Font = font;
		toolStripButton1.Checked = false;
		(sender as ToolStripButton).ForeColor = Color.Red;
		(sender as ToolStripButton).Font = new Font("Microsoft YaHei", 9f, FontStyle.Bold);
		(sender as ToolStripButton).Checked = true;
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ICP_MS.Plugin.Experiment.FormSeqExperiment));
		this.toolStrip1 = new System.Windows.Forms.ToolStrip();
		this.toolStripButton1 = new System.Windows.Forms.ToolStripButton();
		this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
		this.toolStripButton2 = new System.Windows.Forms.ToolStripButton();
		this.panel1 = new System.Windows.Forms.Panel();
		this.toolStripButton3 = new System.Windows.Forms.ToolStripButton();
		this.toolStrip1.SuspendLayout();
		base.SuspendLayout();
		this.toolStrip1.AutoSize = false;
		this.toolStrip1.ImageScalingSize = new System.Drawing.Size(20, 20);
		this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[4] { this.toolStripButton1, this.toolStripSeparator1, this.toolStripButton2, this.toolStripButton3 });
		this.toolStrip1.Location = new System.Drawing.Point(0, 0);
		this.toolStrip1.Name = "toolStrip1";
		this.toolStrip1.Size = new System.Drawing.Size(800, 40);
		this.toolStrip1.TabIndex = 0;
		this.toolStrip1.Text = "toolStrip1";
		this.toolStripButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
		this.toolStripButton1.Image = (System.Drawing.Image)resources.GetObject("toolStripButton1.Image");
		this.toolStripButton1.ImageTransparentColor = System.Drawing.Color.Magenta;
		this.toolStripButton1.Name = "toolStripButton1";
		this.toolStripButton1.Size = new System.Drawing.Size(73, 37);
		this.toolStripButton1.Text = "方法列表";
		this.toolStripButton1.Click += new System.EventHandler(toolStripButton1_Click);
		this.toolStripSeparator1.Name = "toolStripSeparator1";
		this.toolStripSeparator1.Size = new System.Drawing.Size(6, 40);
		this.toolStripButton2.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
		this.toolStripButton2.Image = (System.Drawing.Image)resources.GetObject("toolStripButton2.Image");
		this.toolStripButton2.ImageTransparentColor = System.Drawing.Color.Magenta;
		this.toolStripButton2.Name = "toolStripButton2";
		this.toolStripButton2.Size = new System.Drawing.Size(73, 37);
		this.toolStripButton2.Text = "样品列表";
		this.toolStripButton2.Click += new System.EventHandler(toolStripButton2_Click);
		this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
		this.panel1.Location = new System.Drawing.Point(0, 40);
		this.panel1.Name = "panel1";
		this.panel1.Size = new System.Drawing.Size(800, 410);
		this.panel1.TabIndex = 1;
		this.toolStripButton3.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
		this.toolStripButton3.Image = (System.Drawing.Image)resources.GetObject("toolStripButton3.Image");
		this.toolStripButton3.ImageTransparentColor = System.Drawing.Color.Magenta;
		this.toolStripButton3.Name = "toolStripButton3";
		this.toolStripButton3.Size = new System.Drawing.Size(73, 37);
		this.toolStripButton3.Text = "样品结果";
		base.AutoScaleDimensions = new System.Drawing.SizeF(8f, 15f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(800, 450);
		base.Controls.Add(this.panel1);
		base.Controls.Add(this.toolStrip1);
		base.Name = "FormSeqExperiment";
		base.ShowIcon = false;
		base.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
		this.Text = "序列测试";
		base.WindowState = System.Windows.Forms.FormWindowState.Maximized;
		base.Load += new System.EventHandler(FormSeqExperiment_Load);
		this.toolStrip1.ResumeLayout(false);
		this.toolStrip1.PerformLayout();
		base.ResumeLayout(false);
	}
}
